﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure;
using System.Data.Services.Client;

namespace bdtheque.azure.business.azurestorage
{
    public class CloudTableHelper
    {
        #region Private Members
        private CloudStorageAccount cloudStorageAccount;
        private TableServiceContext tableServiceContext;
        #endregion

        #region Constrcutors
        public CloudTableHelper(CloudStorageAccount _cloudStorageAccount)
        {
            cloudStorageAccount = _cloudStorageAccount;
        }
        #endregion

        #region Properties
        public MessageInProgress Entity { get; set; }
        #endregion

        #region Public Methods
        public void InsertEntity(string tableName, MessageInProgress entity)
        {
            // Create table client
            var tableClient = cloudStorageAccount.CreateCloudTableClient();
            tableClient.RetryPolicy = RetryPolicies.Retry(5, TimeSpan.FromSeconds(5));

            // Get table context
            tableServiceContext = tableClient.GetDataServiceContext();

            // Add Entity and save changes
            tableServiceContext.AddObject(tableName, entity);
            tableServiceContext.SaveChanges();

            Entity = entity;
        }

        public void UpdateEntity()
        {
            // Update Entity and save changes
            tableServiceContext.UpdateObject(Entity);
            tableServiceContext.SaveChanges();
        }

        public void DeleteEntity()
        {
            // Delete Entity and save changes
            tableServiceContext.DeleteObject(Entity);
            tableServiceContext.SaveChanges();
        }

        public T GetEntity<T>(string tableName, string partitionKey, string rowKey) 
            where T : TableServiceEntity
        {
            // Create table client
            var tableClient = cloudStorageAccount.CreateCloudTableClient();
            tableClient.RetryPolicy = RetryPolicies.Retry(5, TimeSpan.FromSeconds(5));

            // Get table context
            tableServiceContext = tableClient.GetDataServiceContext();
            tableServiceContext.IgnoreResourceNotFoundException = true;

            // Create query
            IQueryable<T> entities = (from e in tableServiceContext.CreateQuery<T>(tableName)
                                      where e.PartitionKey == partitionKey && e.RowKey == rowKey
                                      select e);

            // Return found Entity
            return entities.FirstOrDefault();
        }

        public bool WorkInProgress(string messageId, int workInProgressUpdateIntervall)
        {
            Entity = GetEntity<MessageInProgress>("WorkInProgress", "1", messageId);
            
            if (Entity != null && DateTime.Now < Entity.Timestamp.AddSeconds(workInProgressUpdateIntervall))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion
    }
}
